﻿
@model Kooboo.CMS.Web.Areas.Contents.Models.TextContentGrid
@using Kooboo.CMS.Content.Query
@using Kooboo.CMS.Content.Models
@using Kooboo.CMS.Web.Areas.Contents.Controllers
@{
    var schema = (Kooboo.CMS.Content.Models.Schema)ViewData["Schema"];
    var folder = (Kooboo.CMS.Content.Models.TextFolder)ViewData["Folder"];
    var routes = ViewContext.RequestContext.AllRouteValues();
    var folderPermission = Kooboo.CMS.Web.Authorizations.AuthorizationHelpers.Authorize(ViewContext.RequestContext, Kooboo.CMS.Account.Models.Permission.Contents_FolderPermission);
    var allowedEdit = (bool)ViewData["AllowedEdit"];
    var allowedView = (bool)ViewData["AllowedView"];
    this.ViewBag.IsEven = true;
    var parentUUID = ViewContext.RequestContext.AllRouteValues()["parentUUID"] ?? "";
    var pagedList = Model.ContentQuery.ToPageList(Model.PageIndex, Model.PageSize);
    //used in Content_Order
    this.ViewContext.Controller.ViewData["ContentPagedList"] = pagedList;    
}
<h3 class="title">
   @folder.FriendlyText
</h3>
<div class="command clearfix">   
        <div class="dropdown-button">
            <span>@("Create New".Localize())</span>
            <div class="hide">
                <ul>
                    <li>@Html.ActionLink(schema.Name, "Create", ViewContext.RequestContext.AllRouteValues(), new RouteValueDictionary(new { @class = "doc dialog-link", title = "Create ".Localize() + schema.Name }))</li>
                    @if (folderPermission)
                    {
                        <li>@Html.ActionLink("Folder".Localize(), "Create", "TextFolder", ViewContext.RequestContext.AllRouteValues(), new RouteValueDictionary(new { @class = "folder dialog-link", title = "Create Folder".Localize() }))</li>
                    }
                </ul>
            </div>
        </div>
    @Html.ActionLink("Delete".Localize(), "Delete", ViewContext.RequestContext.AllRouteValues(), new RouteValueDictionary(new { @class = "button delete", confirmMsg = "Are you sure you want to remove these items?".Localize(), alertMsg = "Please select items?".Localize() }))
    @if (!string.IsNullOrEmpty(routes["FolderName"] as string) && folderPermission)
    {
        <text>@Html.ActionLink("Setting".Localize(), "Edit", "TextFolder", ViewContext.RequestContext.AllRouteValues().Merge("FullName", routes["FolderName"]).Merge("FolderName", null).Merge("Folder", null), new RouteValueDictionary(new { @class = "button dialog-link", title = "Folder setting".Localize() }))</text>
    }
    @Html.Partial("Search")
</div>
<div class="table-container">
    <table>
        <thead>
            <tr>
                <th class="optional-selector checkbox draggable">
                    <div>
                        <input type="checkbox" class=" select-all" />
                        <ul class="hide">
                            <li>Select:</li>
                            <li class="all"><a href="javascript:;">All Elements</a></li>
                            <li class="docs"><a href="javascript:;">Only Documents</a></li>
                            @if (folderPermission)
                            {
                                <li class="folders"><a href="javascript:;">Only Folders</a></li>
                            }
                            <li class="none"><a href="javascript:;">None</a></li>
                        </ul>
                    </div>
                </th>
                		<th class=" name">Name</th>
		<th class="common englishname">EnglishName</th>
		<th class="common mealtime">MealTime</th>
		<th class="common price">Price</th>
		<th class="common utccreationdate">Date</th>
		<th class="common published">Published</th>

                @if (folder.EmbeddedFolders != null)
                {
                    foreach (var s in folder.EmbeddedFolders)
                    {
                    <th class="action">@Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(new TextFolder(Repository.Current, s)).FriendlyText
                    </th>
                    }
                }
                @if (Repository.Current.EnableWorkflow && folder.EnabledWorkflow)
                {
                    <th class="action">
                        @("Workflow".Localize())
                    </th>
                }
                @if (Repository.Current.AsActual().EnableVersioning.Value == true)
                {
                    <th class="action">
                        @("Versions".Localize())
                    </th>
                }
                <th class="action">
                    @("Edit".Localize())
                </th>
                @if (schema.IsTreeStyle)
                {
                    <th class="action">
                    </th> 
                }
            </tr>
        </thead>
        @if (Model.ChildFolders != null)
        {
            <tbody>
                @foreach (dynamic item in Model.ChildFolders)
                {
                    <tr class="folderTr">
                        <td class="undraggable">
                            @if (folderPermission)
                            {
                                <input type="checkbox" name="Selected" class="select folders" id="@item.FullName" value="@item.FullName" />
                            }
                        </td>
                        <td>
                            @if (!string.IsNullOrEmpty(item.SchemaName))
                            {
                                <a class="f-icon folder" href="@this.Url.Action("Index", ViewContext.RequestContext.AllRouteValues().Merge("FolderName", (object)(item.FullName)).Merge("FullName", (object)(item.FullName)))" >
                                    @Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(item).FriendlyText</a>
                            }
                            else
                            {
                                <a class="f-icon folder" href="@this.Url.Action("Index", ViewContext.RequestContext.AllRouteValues().Merge("controller", "TextFolder").Merge("FolderName", (object)(item.FullName)).Merge("FullName", (object)(item.FullName)))" >
                                    @Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(item).FriendlyText</a>
                            }
                        </td>
                        <td colspan="5">
                        </td>
                        @if (Repository.Current.EnableWorkflow && folder.EnabledWorkflow)
                        {
                            <td colspan="1">
                            </td>
                        }
                        @if (Repository.Current.AsActual().EnableVersioning.Value == true)
                        {
                            <td colspan="1">
                            </td>
                        }
                        @if (folder.EmbeddedFolders != null)
                        {
                            <td colspan="@folder.EmbeddedFolders.Count()">
                            </td>
                        }
                        <td class="action">
                            @if (folderPermission)
                            {
                                <a class="o-icon edit dialog-link" title ="@("Edit folder".Localize())" href="@this.Url.Action("Edit", "TextFolder", ViewContext.RequestContext.AllRouteValues().Merge("FolderName", (object)(item.FullName)).Merge("FullName", (object)(item.FullName)))" >
                                    Edit</a>
                            }
                        </td>
                        @if (schema.IsTreeStyle)
                        {
                            <td class="action">
                            </td>
                        }
                    </tr>
                }
            </tbody>
        }
        <tbody>
            @AddTreeItems(pagedList, schema, allowedEdit, folder, true)
        </tbody>
    </table>
    <div class="pagination">
        @Html.Pager(pagedList)
    </div>
</div>
<script language="javascript" type="text/javascript">
kooboo.cms.content.textcontent.initGrid('@("Are you sure you want to delete these items?".Localize())', '@("You have not select any item!".Localize())');
</script>
@helper AddTreeItems(IEnumerable<TextContent> items, Schema schema, bool allowedEdit, TextFolder folder, bool isRoot)
    {
        if (Repository.Current.EnableWorkflow == true)
        {
            items = Kooboo.CMS.Content.Services.ServiceFactory.WorkflowManager.GetPendWorkflowItemForContents(Repository.Current, items.ToArray(), User.Identity.Name);
        }
        foreach (dynamic item in items)
        {
            this.ViewBag.IsEven = (!this.ViewBag.IsEven);
            var workflowItem = item._WorkflowItem_;
            var hasworkflowItem = workflowItem != null;
            var availableEdit = hasworkflowItem || (!hasworkflowItem && allowedEdit);
            var editTitle = "Edit ".Localize() + schema.Name + ((item.IsLocalized != null && item.IsLocalized == false) ? " From:".Localize() + item.OriginalRepository.ToString() + "." + item.OriginalFolder.ToString() : "");
    <tr pid="@item.ParentUUID" id="node-@item.UUID" class= "@(this.ViewBag.IsEven ? "even " : "")@((!isRoot && !string.IsNullOrEmpty(item.ParentUUID)) ? "child-of-node-" + item.ParentUUID : "") @((item.IsLocalized != null && item.IsLocalized == false) ? "unlocalized" : "") @(hasworkflowItem ? "hasworkflowItem" : "")" >
        <td class="draggable" style="padding">
            @if (availableEdit)
            {
                <input type="checkbox" name="Selected" class="select docs" id="@item.UUID" value="@item.UUID" />
            }
        </td>
       		<td><a class="f-icon document dialog-link " title="@editTitle" href="@this.Url.Action("Edit","TextContent",ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID",(object)(item.UUID)))" >@(item.Name ?? "")</a></td>
		<td>@(item.EnglishName ?? "")</td>
		<td>@(item.MealTime ?? "")</td>
		<td>@(item.Price ?? "")</td>
		<td class="date">@(DateTime.Parse(item["UtcCreationDate"].ToString()).ToLocalTime().ToShortDateString())</td>
		<td class="action">@if(allowedEdit){<a href="@Url.Action("Publish","TextContent",ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID",(object)(item.UUID)))" class="boolean-ajax-link o-icon @((item.Published!=null && item.Published == true)?"tick":"cross")" confirmMsg="@("Are you sure you want to publish this item?".Localize())"  unconfirmMsg="@("Are you sure you want to unpublish this item?".Localize())"></a>} else {<span class='o-icon @((item.Published!=null && item.Published == true)?"tick":"cross")'></span>}</td>
        @if (folder.EmbeddedFolders != null)
        {
            foreach (var s in folder.EmbeddedFolders)
            {
                var embeddedFolder = Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(new TextFolder(Repository.Current, s));
            <td class="action">
                @Html.ActionLink(embeddedFolder.FriendlyText + " (" + ((TextContent)item).Children(s).Count() + ")", "SubContent", "TextContent", new { SiteName = ViewContext.RequestContext.GetRequestValue("SiteName"), RepositoryName = ViewContext.RequestContext.GetRequestValue("RepositoryName"), ParentFolder = ViewContext.RequestContext.GetRequestValue("FolderName"), Folder = s, FolderName = s, parentUUID = (object)(item.UUID) }, new { @class = "dialog-link", title = embeddedFolder.FriendlyText })
            </td>
            }
        }
        @if (Repository.Current.EnableWorkflow && folder.EnabledWorkflow)
        {
            <td class="action">
                @if (hasworkflowItem)
                {
                    <a href="@Url.Action("Process", "PendingWorkflow", ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID", (object)(item.UUID)).Merge("RoleName", (object)(workflowItem.RoleName)).Merge("Name", (object)(workflowItem.Name)))" title="@("Process workflow".Localize())" class="o-icon process dialog-link">@("Process workflow".Localize())</a>
                }
                else
                {
                    <a href="@Url.Action("WorkflowHistory", "PendingWorkflow", ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID", (object)(item.UUID)))" title="@("View workflow history".Localize())" class="o-icon workflow dialog-link">@("View workflow history".Localize())</a>                      
                }
            </td>
        }
        @if (Repository.Current.AsActual().EnableVersioning.Value == true)
        {
            <td class="action ">
                <a class="o-icon version dialog-link" title="@("Versions".Localize())" href="@this.Url.Action("Versions", ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID", (object)(item.UUID)))" >@("Version").Localize())</a>
            </td>
        }
        <td class="action">
            <input type="hidden" name="Sequence" value="@item.Sequence"/>
            <a class="o-icon edit dialog-link" title="@editTitle" href="@this.Url.Action("Edit", ViewContext.RequestContext.AllRouteValues().Merge("UserKey", (object)(item.UserKey)).Merge("UUID", (object)(item.UUID)))" >@("Edit".Localize())</a>
        </td>@if (schema.IsTreeStyle)
         {<td class="action">
            <a class="o-icon add dialog-link" title="Create a sub item" href="@this.Url.Action("Create", ViewContext.RequestContext.AllRouteValues().Merge("ParentUUID", (object)(item.UUID)))" >@("Create".Localize())</a>
        </td>}
    </tr>
    var query = folder.CreateQuery().WhereEquals("ParentUUID", ((TextContent)item).UUID).DefaultOrder();
    @AddTreeItems(query.ToArray(), schema, allowedEdit, folder, false);
        }
}
@Html.Partial("Content_Order", pagedList, ViewData)
